
from typing import Callable, Generator


class LLMcontroller:
    def __init__(self):
        self.model_registry={}
        self.response_generator=None
    
    def register_model(self,name:str,handler_factory:Callable[...,Callable[[str],Generator[str,None,None]]]):
        self.model_registry[name] = handler_factory

    def acceptQuery(self, query: str, provider: str, **kwargs):
        if provider not in self.model_registry:
            raise ValueError(f"Model '{provider}' is not registered.")
        handler = self.model_registry[provider](**kwargs)  # 动态注入 model, api_key, base_url
        self.response_generator = handler(query)

    def stream(self):
        if self.response_generator is None:
            return
        try:
            while True:
                yield next(self.response_generator)
        except StopIteration:
            return
